
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>

pthread_mutex_t mutex;
int ticket=100;


void *buy_ticket(void *arg)
{
	const char *id=(const char *)arg;
	while(1){
		pthread_mutex_lock(&mutex);
		if(ticket>0){
			usleep(1000);
			printf("%s get a ticket:%d\n",id,ticket);
			ticket--;
			pthread_mutex_unlock(&mutex);
		}
		else 
			pthread_mutex_unlock(&mutex);
			break;
	}
}
int main()
{
	pthread_t tid1,tid2,tid3,tid4;

	pthread_mutex_init(&mutex,NULL);
	
	pthread_create(&tid1,NULL,buy_ticket,"thread_1");
	pthread_create(&tid2,NULL,buy_ticket,"thread_2");
	pthread_create(&tid3,NULL,buy_ticket,"thread_3");
	pthread_create(&tid4,NULL,buy_ticket,"thread_4");

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_join(tid3,NULL);
	pthread_join(tid4,NULL);

	pthread_mutex_destroy(&mutex);
	return 0;
}
